Skip to content

Conversation

@andrewm4894
Copy link
Contributor

@andrewm4894 andrewm4894 commented Oct 24, 2025

Summary

Adds a simple $ai_framework property to AI events, only when an actual framework layer is used (e.g., LangChain). This helps identify which abstraction layer is being used on top of the underlying AI provider.

Changes

  • Add $ai_framework property to AI events for framework integrations (LangChain)
  • Direct provider calls (OpenAI, Anthropic, Gemini) do not include this property
  • Property contains a simple string value (e.g., "langchain")

Example Events

Framework call (LangChain):

{
  "$ai_framework": "langchain",
  "$ai_provider": "openai",
  "$ai_model": "gpt-4",
  ...
}

Direct provider call (OpenAI):

{
  "$ai_provider": "openai",
  "$ai_model": "gpt-4",
  ...
}

Rationale

This approach eliminates redundancy - we only add $ai_framework when there's an actual framework layer. For direct provider calls, the framework would just duplicate the provider name, which is wasteful.

Testing

  • All existing tests updated and passing (119 passed, 8 skipped)
  • Added assertions for $ai_framework in LangChain tests
  • Verified property is not present in direct provider tests

Adds framework identification metadata to all AI events for easier filtering
and analytics. Each integration now includes a $ai_lib_metadata property with
schema version and framework name.

- LangChain: Hardcoded to "langchain"
- Native wrappers (Anthropic, OpenAI, Gemini): Uses provider name
- Ready for future frameworks (pydantic-ai, crewai, llamaindex)

This enables PostHog queries to easily distinguish between:
- Direct SDK wrapper usage
- Framework-mediated usage (LangChain, etc.)
- Different framework types

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Added \$ai_lib_metadata to call_llm_and_track_usage (sync)
- Added \$ai_lib_metadata to call_llm_and_track_usage_async (async)
- Added test assertion in test_basic_completion
- Placed metadata at end of properties for consistency

All tests pass successfully.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@andrewm4894 andrewm4894 changed the title Add $ai_lib_metadata to AI integrations feat(ai): Add $ai_lib_metadata to AI integrations Oct 24, 2025
Creates a single `get_ai_lib_metadata(framework)` utility function to generate
the $ai_lib_metadata object, replacing inline implementations across the
codebase.

Changes:
- Add get_ai_lib_metadata() utility to utils.py
- Update LangChain callbacks to use utility function
- Update call_llm_and_track_usage() to use utility function
- Update call_llm_and_track_usage_async() to use utility function
- Update capture_streaming_event() to use utility function

Benefits:
- Consistency across all integrations
- Single source of truth for metadata structure
- Easier to extend with version detection later

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@andrewm4894 andrewm4894 requested a review from a team October 24, 2025 15:38
@andrewm4894 andrewm4894 marked this pull request as ready for review October 24, 2025 15:38
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines 482 to 491

assert call_args["distinct_id"] == "test-id"
assert call_args["event"] == "$ai_generation"
assert props["$ai_lib_metadata"] == {
"schema": "v1",
"frameworks": [{"name": "openai"}],
}
assert props["$ai_provider"] == "openai"
assert props["$ai_model"] == "gpt-4"
assert props["$ai_input"] == [{"role": "user", "content": "Hello"}]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Check that Anthropic, Gemini, and LangChain tests also assert $ai_lib_metadata

This test correctly validates the new metadata field, but similar assertions are missing in other provider tests:

  • posthog/test/ai/anthropic/test_anthropic.py:276 (test_basic_completion)
  • posthog/test/ai/gemini/test_gemini.py:147 (test_new_client_basic_generation)
  • posthog/test/ai/langchain/test_callbacks.py:160 (test_basic_chat_chain)
Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/test/ai/openai/test_openai.py
Line: 482:491

Comment:
**style:** Check that Anthropic, Gemini, and LangChain tests also assert `$ai_lib_metadata`

This test correctly validates the new metadata field, but similar assertions are missing in other provider tests:
- `posthog/test/ai/anthropic/test_anthropic.py:276` (`test_basic_completion`)
- `posthog/test/ai/gemini/test_gemini.py:147` (`test_new_client_basic_generation`)  
- `posthog/test/ai/langchain/test_callbacks.py:160` (`test_basic_chat_chain`)

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add missing $ai_lib_metadata assertions to Anthropic, Gemini, and LangChain tests to match the validation already present in OpenAI tests. Each test now verifies the metadata field contains the correct schema version and framework name.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link
Member

@Radu-Raicea Radu-Raicea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! 🎉

Copy link
Member

@Radu-Raicea Radu-Raicea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to update version.py and the changelog!

Changes:
- Replace complex $ai_lib_metadata object with simple $ai_framework string
- Only include $ai_framework when using actual framework (LangChain)
- Remove $ai_framework from direct provider calls (OpenAI, Anthropic, Gemini)
- Update all tests to reflect new behavior

Before: {"schema": "v1", "frameworks": [{"name": "langchain"}]}
After: "langchain" (only when using LangChain framework)

This eliminates wasteful redundancy where framework=provider for direct calls.
@andrewm4894 andrewm4894 changed the title feat(ai): Add $ai_lib_metadata to AI integrations feat(ai): Add $ai_framework property for framework integrations Oct 28, 2025
@andrewm4894 andrewm4894 changed the title feat(ai): Add $ai_framework property for framework integrations feat(ai): Add $ai_framework property for framework integrations Oct 28, 2025
@andrewm4894 andrewm4894 added the enhancement New feature or request label Oct 28, 2025
@andrewm4894 andrewm4894 merged commit edfadcc into master Oct 28, 2025
10 checks passed
@andrewm4894 andrewm4894 deleted the feature/ai-lib-metadata branch October 28, 2025 09:59
andrewm4894 added a commit that referenced this pull request Oct 28, 2025
andrewm4894 added a commit that referenced this pull request Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request team/llm-analytics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants